Week 11: Input devices
The assignment:
1. Group assignemt: LINK
2. Individual assignment: Measure something: add a sensor to a microcontroller board that you have designed and read it
In class we learned what a sensor is, different sensor types and how to choose them e.g. sensors that measure temperature, light, motion, distance, location, orientation, moisture and many other things. We also were presented to 3 rules for every project:
What I did
First I wanted to learn about the DHT11 (temperature and moisture sensor) and the data it could read. I looked through some webpages to do so. I started here
Among other things I could read that the average humidity is 60%, meaning that 60% of the air around me is water vapor. I made some further research and found that the ideal humidity range should be between 40% and 60%. Sources to support this:
This is useful information for my final project. I could also read about how to prepare the code for the sensor in Arduino IDE. I did the following:
The data pin from the sensor is connected to my board with pin 0. I corrected this in the code.
Now I was ready to upload the code to my ATtiny board to give me some data on the temperature and the humidity in the room. This is what I did to program the ATtiny with a programmer. I connected this programmer:
with the AatTiny board:
See the design file (KiCad) for the board here
I checked the settings for board, port and programmer and then I uploaded the code from the library using >sketch>upload using programmer. Problems: I got some error messages here, but in the end I made it work
Mistakes made and lessons learned:
Nevertheless I could make a serial connection.
The sensor started to read the temperature and the humidity in the room. To test it I breathed on the sensor and in the serial monitor I could read this:
Combining input and output
I made a test where I wanted a certain threshold in the reading from the sensor to move a sensor. I took the code from input week and the code from output week and adjusted it with this peace of code:
Explanation of the code: To begin with I include the library for the sensor DHT11 and the servo in the code by writing: #include DHT11.h #include Servo.h Servo myservo;
In void setup (runs only once) the baut rate is set to 9600 bits per second. This allows serial communication between the computer, the port and my board. I also define the pin number on which the servo is connected. In this case it is pin number 10 myservo.attach(10);
Under void loop (runs continously) I write how I want the servo to move based on humidity. In this case I want the servo to move when the humidity level is 60% or higher. if (humidity >= 60) { for (int pos = 0; pos <= 90; pos += 1) { // goes from 0 degrees to 180 degrees // in steps of 1 degree
And when the humidity level drops under 58% I want the servo to stop moving
if (humidity <= 58) { for (int pos = 90; pos >= 0; pos -= 1) { // goes from 90 degrees to 0 degreesThis it what came out of it: